package com.file.parser;

import java.io.File;
import java.io.IOException;

import com.file.bo.DomesticDiploma;
import com.file.bo.ResponseData;
import com.file.constant.ErrorCode;
import com.file.util.JsonUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ChsiHtmlParser {
	
	public ResponseData<String> parseChsiHtmlToJson(String filePath) {
		log.info("parseXlcxHtmlToJson started, filePath:{}", filePath);
		String json = null;

		try {
			DomesticDiploma diploma = parseChsiHtml(filePath);
			json = JsonUtils.convertObjectToJson(diploma);
		} catch (Exception e) {
			log.error("[OnError]parseXlcxHtmlToJson failed, filePath:{}", filePath, e);
			return new ResponseData<String>(json, ErrorCode.FILE_PARSE_EXCEPTION.getCode(),
					ErrorCode.FILE_PARSE_EXCEPTION.getMsg());
		}

		log.info("parseXlcxHtmlToJson completed, json:{}", json);
		return new ResponseData<String>(json, ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg());
	}
	
	public DomesticDiploma parseChsiHtml(String filePath) throws IOException {
		File input = new File(filePath);
		DomesticDiploma domesticDiploma = new DomesticDiploma();

		Document doc = Jsoup.parse(input, "UTF-8");
		Element span = doc.select("div.img-right > h6 > span").first();
		domesticDiploma.setSex(span.text());
		
		Element h6 = doc.select("div.img-right > h6").first();
		domesticDiploma.setName(h6.text().substring(0, h6.text().length()-1));
		
		Element p = doc.select("div.img-right > p").first();
		domesticDiploma.setDateOfBirth(p.text());
		
		Element div0 = doc.select("div.pw-content > div").first();
		
		Elements divChildren = div0.children();
		for (Element element : divChildren) {
			System.out.println(element.text());
			switch (element.child(0).text()) {
			case "入学日期":
				String dateOfEnrollment = element.child(1).text();
				domesticDiploma.setDateOfEnrollment(dateOfEnrollment);
				break;
			case "毕(结)业日期":
				String dateOfGraduation = element.child(1).text();
				domesticDiploma.setDateOfGraduation(dateOfGraduation);
				break;
			case "学校名称":
				String schoolName = element.child(1).text();
				domesticDiploma.setSchoolName(schoolName);
				break;
			case "专业":
				String major = element.child(1).text();
				domesticDiploma.setMajor(major);
				break;
			case "学历类别":
				String diplomaClassification = element.child(1).text();
				domesticDiploma.setDiplomaClassification(diplomaClassification);
				break;
			case "学制":
				String schoolSystem = element.child(1).text();
				domesticDiploma.setSchoolSystem(schoolSystem);
				break;
			case "学习形式":
				String learningStyle = element.child(1).text();
				domesticDiploma.setLearningStyle(learningStyle);
				break;
			case "层次":
				String degree = element.child(1).text();
				domesticDiploma.setDegree(degree);
				break;
			case "毕(结)业":
				String graduation = element.child(1).text();
				domesticDiploma.setGraduation(graduation);
				break;
			case "校(院)长姓名":
				String principalName  = element.child(1).text();
				domesticDiploma.setPrincipalName(principalName);
				break;
			case "证书编号":
				String certificateNo  = element.child(1).text();
				domesticDiploma.setCertificateNo(certificateNo);
				break;
			case "证件号码":
				String idNo  = element.child(1).text();
				domesticDiploma.setIdNo(idNo);
				break;
			case "补证学校":
				String supplementaryCertificateSchool  = element.child(1).text();
				domesticDiploma.setSupplementaryCertificateSchool(supplementaryCertificateSchool);;
				break;
			case "补证日期":
				String supplementaryCertificateDate  = element.child(1).text();
				domesticDiploma.setSupplementaryCertificateDate(supplementaryCertificateDate);;
				break;
			case "补证编号":
				String supplementaryCertificateNo  = element.child(1).text();
				domesticDiploma.setCertificateNo(supplementaryCertificateNo);
				break;
			}
		}
		
		log.info(domesticDiploma.toString());
		return domesticDiploma;
	}
	
	public static void main(String[] args) {
		String filePath = "E:\\data\\file\\chsi\\chsi_xlzm.html";
		ChsiHtmlParser chsiHtmlParser = new ChsiHtmlParser();
		String json = chsiHtmlParser.parseChsiHtmlToJson(filePath).getData();
		log.info(json);

	}

}
